<?php

/**
 * @author AnhVu, PhatPham
 * @copyright 2013
 */
class Questionmodel extends CI_Model {
    var $question_id;   //qid
    var $reused_question_id;    //rqid
    var $content;   //content
    var $view_order;    //vorder
    var $view_style;    //vstyle
    var $max_option;    //maxopt
    var $is_vocation;   //isvoc
    var $start_hide;    //shide
    var $required;  //required
    var $is_validated;  //isval
    var $is_evaluated;  //iseval
    var $survey_id; //sid
    var $is_deleted;
    var $created_by_user_id;    //userid
    var $created_on_date;
    var $last_modified_by_user_id;  //userid
    var $last_modified_on_date;
    
    function __construct(){
        // Call the Model constructor
        $this->load->database();
    }
    //Get questions list
    function get_questions_list($sid){
        $this->db->order_by('view_order', 'asc');
        $query = $this->db->get_where('sur_question', array('survey_id' => $sid, 'is_deleted' => false));
        return $query;
    }
    //Get questions list after current question
    function get_questions_list_after($sid, $vorder){
        $this->db->order_by('view_order', 'asc');
        $query = $this->db->get_where('sur_question', array('survey_id' => $sid, 'view_order >=' => $vorder,'is_deleted' => false));
        return $query;
    }
	//Get questions list before current question
    function get_questions_list_before($sid, $vorder){
        $this->db->order_by('view_order', 'asc');
        $query = $this->db->get_where('sur_question', array('survey_id' => $sid, 'view_order <' => $vorder,'is_deleted' => false));
        return $query;
    }
	//Get questions list from ... to ...
    function get_questions_list_from_to($sid, $from, $to){
        $this->db->order_by('view_order', 'asc');
        $query = $this->db->get_where('sur_question', array('survey_id' => $sid, 'view_order >=' => $from, 'view_order <=' => $to,'is_deleted' => false));
        return $query;
    }
    //Get question of
    function get_question($qid){
        $query = $this->db->get_where('sur_question', array('question_id' => $qid, 'is_deleted' => false));
        if($query->num_rows() > 0){
            return $query->row();
        }
        return null;
    }
    //Create question
    function create_question($qid, $rqid, $content, $vorder, $vstyle, $maxopt, $shide, $required, $isval, $iseval, $sid, $userid){
        $data = array(
               'question_id' => $qid,
               'reused_question_id' => $rqid,
               'content' => $content,
               'view_order' => $vorder,
               'view_style' => $vstyle,
               'max_option' => $maxopt,
               'is_vocation' => null,
               'start_hide' => $shide,
               'required' => $required,
               'is_validated' => $isval,
               'is_evaluated' => $iseval,
               'survey_id' => $sid,
               'is_deleted' => false,
               'created_by_user_id' => $userid,
               'created_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->insert('sur_question', $data);
    }
    //Delete question
    function delete_survey_questions($sid, $userid){
        $data = array(
               'is_deleted' => true,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_question', $data);
    }
    function delete_question($qid, $userid){
        $data = array(
               'is_deleted' => true,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('question_id', $qid);
        $this->db->update('sur_question', $data);
    }
    //Edit question
    function edit_question($qid, $content, $vorder, $vstyle, $maxopt, $shide, $required, $userid){
        $data = array(
               'content' => $content,
               'view_order' => $vorder,
               'view_style' => $vstyle,
               'max_option' => $maxopt,
			   'start_hide' => $shide,
               'required' => $required,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('question_id', $qid);
        $this->db->update('sur_question', $data);
    }
    //Edit question view order
    function edit_question_view_order($qid, $vorder, $userid){
        $data = array(
               'view_order' => $vorder,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('question_id', $qid);
        $this->db->update('sur_question', $data);
    }
    //Edit question is_validated
    function edit_question_validated($qid, $isval, $userid){
        $data = array(
               'is_validated' => $isval,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('question_id', $qid);
        $this->db->update('sur_question', $data);
    }
    //Edit question is_validated
    function edit_question_evaluated($qid, $iseval, $userid){
        $data = array(
               'is_evaluated' => $iseval,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('question_id', $qid);
        $this->db->update('sur_question', $data);
    }
}
?>